﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using EDMDesigner.Business.CSDL.Property.Complex;
using EDMDesigner.Business.CSDL.Property.Navigation;
using EDMDesigner.Business.CSDL.Property.Scalar;
using EDMDesigner.Business.CSDL.Util;
using EDMDesigner.Business.Util;
using EDMDesigner.Business.CSDL.Property;

namespace EDMDesigner.Business.CSDL.Type
{
    public abstract class TypeBase : INotifyPropertyChanged, INamedElement
    {
        internal TypeBase()
        {
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        public Visibility _visibility;
        [DisplayName("Access")]
        public Visibility Visibility
        {
            get { return _visibility; }
            set
            {
                _visibility = value;
            }
        }

        private EventedObservableCollection<ScalarProperty> _scalarProperties;
        public EventedObservableCollection<ScalarProperty> ScalarProperties
        {
            get
            {
                if (_scalarProperties == null)
                {
                    _scalarProperties = new EventedObservableCollection<ScalarProperty>();
                    _scalarProperties.ItemAdded += scalarProperty => scalarProperty.EntityType = this;
                    _scalarProperties.ItemRemoved += scalarProperty => OnScalarPropertyRemoved(scalarProperty);
                }
                return _scalarProperties;
            }
        }

        protected virtual void OnScalarPropertyRemoved(ScalarProperty scalarProperty)
        {
        }

        private EventedObservableCollection<ComplexProperty> _complexProperties;
        public EventedObservableCollection<ComplexProperty> ComplexProperties
        {
            get
            {
                if (_complexProperties == null)
                {
                    _complexProperties = new EventedObservableCollection<ComplexProperty>();
                    _complexProperties.ItemAdded += complexProperty => complexProperty.EntityType = this;
                }
                return _complexProperties;
            }
        }

        public virtual IEnumerable<ScalarProperty> AllScalarProperties
        {
            get
            {
                return ScalarProperties;
            }
        }
        public virtual IEnumerable<ComplexProperty> AllComplexProperties
        {
            get
            {
                return ComplexProperties;
            }
        }

        internal CSDLContainer Container { get; set; }

        public virtual PropertyBase DuplicateProperty(PropertyBase property)
        {
            var propertyBase = property.Duplicate();
            propertyBase.AddToType(this);
            return propertyBase;
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public override string ToString()
        {
            return Name;
        }
    }
}
